package com.smooth.site.sx.timer;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.smooth.dao.gen.AIndustryMapper;
import com.smooth.dao.gen.BaseCategoryMapper;
import com.smooth.dao.gen.bean.AIndustry;
import com.smooth.dao.gen.bean.AIndustryExample;
import com.smooth.dao.gen.bean.BaseCategory;
import com.smooth.dao.gen.bean.BaseCategoryExample;
import com.smooth.site.sx.mag.basedata.category.enums.CategoryType;

public class CompCategoryDataMain {
	public static void main(String[] args) {
		Logger log = LoggerFactory.getLogger(CompCategoryDataMain.class);
		try {
			String springFileXml = "classpath*:spring-context*.xml";
			log.info(springFileXml);
			ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(springFileXml);

			AIndustryMapper industryMapper = applicationContext.getBean(AIndustryMapper.class);
			BaseCategoryMapper baseCategoryMapper = applicationContext.getBean(BaseCategoryMapper.class);
			
			List<AIndustry> alist = industryMapper.selectByExample(new AIndustryExample());
			for(AIndustry industry : alist) {
				BaseCategory baseCategoryDb = baseCategoryMapper.selectByPrimaryKey(industry.getIndustryId()+"");
				if(baseCategoryDb==null) {
					Integer level = industry.getIndustryLevel();
					
					Date date = new Date();
					BaseCategory baseCategory = new BaseCategory();
					baseCategory.setCategoryType(CategoryType.company.value);
					baseCategory.setId(industry.getIndustryId()+"");
					baseCategory.setUpdateDate(date);
					baseCategory.setCreateDate(date);
					
					if(level==1) {
						baseCategory.setCategoryName(industry.getIndustryOne());
						baseCategory.setParentId("");
						int optNum = baseCategoryMapper.insertSelective(baseCategory);
						if(optNum==1) {
							log.info(baseCategory.getCategoryName()+" 插入完成");
						}
					}else if(level==2) {
						BaseCategoryExample baseCategoryExample = new BaseCategoryExample();
						baseCategoryExample.createCriteria().andCategoryNameEqualTo(industry.getIndustryOne());
						List<BaseCategory> blist = baseCategoryMapper.selectByExample(baseCategoryExample);
						if(blist.size()>0) {
							baseCategory.setParentId(blist.get(0).getId());
							baseCategory.setCategoryName(industry.getIndustryTwo());
							int optNum = baseCategoryMapper.insertSelective(baseCategory);
							if(optNum==1) {
								log.info(baseCategory.getCategoryName()+" 插入完成");
							}
						}else {
							
						}
					}
				}else {
					log.info("该条数据已经存在无须更新");
				}
			}
		} catch (Exception e) {
			log.error("", e);
		}
	}

}
